<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户列表</title>
    <style>

        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            padding: 20px;
        }

        .table-container {
            width: 100%;
            max-width: none;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            box-sizing: border-box;
        }

        h2 {
            text-align: center;
            color: #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        thead {
            background-color: #f2f2f2;
        }

        th, td {
            padding: 12px;
            text-align: center;
            border: 1px solid #ddd;
        }

        tr:nth-child(even) {
            background-color: #fafafa;
        }

        tr:hover {
            background-color: #e0e0e0;
        }

        .button-group {
            margin-top: 15px;
            padding: 10px;
            background-color: #f8f9fa; /* 添加浅灰色背景 */
            border-radius: 4px;
            text-align: right; /* 右对齐按钮 */
        }

        .button-group button {
            padding: 8px 16px;
            margin: 0 2px; /* 原来是 10px，改为 5px */
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        .button-group button:hover {
            background-color: #0056b3;
        }

        /* 选中行的高亮样式 */
        .selected-row {
            background-color: #d1ecf1 !important; /* 浅蓝色背景 */
            font-weight: bold;
        }

        .pagination-container {
            text-align: right;
        }

    </style>

    <script>
        let selectedUserId = null;
        let previouslySelectedRow = null;

        function selectUser(userId, element) {
            selectedUserId = userId;

            // 移除之前选中的行的高亮
            if (previouslySelectedRow) {
                previouslySelectedRow.classList.remove("selected-row");
            }

            // 给当前点击的行加上高亮
            element.classList.add("selected-row");

            // 保存当前行，方便下次移除
            previouslySelectedRow = element;
        }

        function editUser(userId) {
            if (!userId) return;
            window.location.href = "/user/editUser/" + userId;
        }

        function deleteUser() {
            if (selectedUserId) {
                if (confirm("确定要删除该用户吗？")) {
                    window.location.href = "/user/deleteUser/" + selectedUserId;
                }
            } else {
                alert("请选择一个用户");
            }
        }
    </script>
</head>
<body>
<div class="table-container">
    <h2>用户列表</h2>
    <div class="button-group">
        <button type="button" onclick="location.href='{% url 'addUser' %}'">新增</button>
        <button type="button" onclick="if(selectedUserId) editUser(selectedUserId); else alert('请选择用户')">修改
        </button>
        <button type="button" onclick="if(selectedUserId) deleteUser();else alert('请选择用户')">删除</button>
    </div>
    <table>
        <thead>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>邮箱</th>
        </tr>
        </thead>
        <tbody>
        {% for user in users %}
            <tr onclick="selectUser({{ user.id }},this)">
                <td>{{ user.name }}</td>
                <td>{{ user.sex }}</td>
                <td>{{ user.mail }}</td>
            </tr>
        {% empty %}
            <tr>
                <td colspan="3">暂无用户</td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
</div>
<!-- 分页导航容器 -->
<div class="pagination-container">
    {% include "pagination.html" %}
</div>
</body>
</html>